<?php

/*
 * class.photosdao.php
 *
 */

include_once('framework/DataAccessObject.php');

class PhotosDAO extends DataAccessObject
{
	// Add new entry to Photos.
	function addPhoto($title, $datatype, $datasize, $data, $date, $description)
	{
		$this->db->execute("INSERT INTO photos VALUES (null, ?, ?, ?, ?, ?, ?)", array($datatype, $datasize, $data, $title, $date, $description));
		// return id
		return $this->db->executeField("SELECT id FROM photos WHERE id=LAST_INSERT_ID()", array());
	}

	function deleteEntry($photoId)
	{
		$this->db->execute("DELETE FROM photos WHERE id=?", array($photoId));
	}


	function isExisting($id)
	{
		if ($this->db->executeField("SELECT id FROM photos WHERE id=?", array($id)) != "")
		{
			return true;
		}
		return false;
	}


	function numberOfEntries()
	{
		return $this->db->executeField("SELECT COUNT(*) FROM photos", array());
	}

	// Get function for photo data
	function getData($photo_id)
	{
		return $this->db->executeRow("SELECT data_size,data_type,data FROM photos WHERE id=?", array($photo_id));
	}


	// Set/Update functions for title
	function getTitle($photo_id)
	{
		return $this->db->executeField("SELECT title FROM photos WHERE id=?", array($photo_id));
	}

	function updateTitle($photo_id, $new_title)
	{
		return $this->db->execute("UPDATE photos SET title=? WHERE id=?", array($new_title, $photo_id));
	}


	// Set/Update functions for description
	function getDescription($photo_id)
	{
		return $this->db->executeField("SELECT description FROM photos WHERE id=?", array($photo_id));
	}

	function updateDescription($photo_id, $new_description)
	{
		return $this->db->execute("UPDATE photos SET description=? WHERE id=?", array($new_description, $photo_id));
	}


	// Set/Update functions for date
	function getSubmitDate($photo_id)
	{
		return $this->db->executeField("SELECT submit_date FROM photos WHERE id=?", array($photo_id));
	}

	function updateSubmitDate($photo_id, $new_date)
	{
		return $this->db->execute("UPDATE photos SET submit_date=? WHERE id=?", array($new_date, $photo_id));
	}

	function getInterval($offset, $limit)
	{
		return $this->db->executeSet("SELECT id, title, submit_date, description FROM photos ORDER BY submit_date LIMIT ? OFFSET ?", array($limit, $offset));
	}


	function getLastId()
	{
		return $this->db->executeField("SELECT MAX(id) FROM photos", array());
	}

	function getNextId($id)
	{
		return $this->db->executeField("SELECT id FROM photos WHERE id > ? ORDER BY id ASC LIMIT 1", array($id));
	}

	function getPreviousId($id)
	{
		return $this->db->executeField("SELECT id FROM photos WHERE id < ? ORDER BY id DESC LIMIT 1", array($id));
	}

	function getAllEntries()
	{
		return $this->db->executeSet("SELECT id FROM photos WHERE 1 ORDER BY submit_date DESC", array()); 
	}

	function getEntries($year, $month)
	{
		return $this->db->executeSet("SELECT id, DAY(submit_date) FROM photos WHERE YEAR(submit_date) = ? AND MONTH(submit_date) = ?", array($year, $month));
	}


}

?>
